package projectmanagement.console;

import projectmanagement.models.User;

import java.util.Collection;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * @author Dmitry Toryanik
 *         <p/>
 *         Users handling class handles and renders
 *         menu options which relates to users. Has a custom
 *         constructors which takes the console manager class
 *         to traversing entire application menus.
 */
public class UsersHandling {
    private Scanner scanner = new Scanner(System.in);
    private ConsoleManager consoleManager;

    public UsersHandling(ConsoleManager consoleManager) {
        this.consoleManager = consoleManager;
    }

    void createUser() {
        System.out.println("Creating user...\n");

        System.out.print("User name: ");
        String userName = scanner.nextLine().trim();
        System.out.print("Age: ");
        int age = 0;

        try {
            age = scanner.nextInt();
        } catch (InputMismatchException e) {
            ConsoleManager.goBackHandler("User's age must be an integer!", scanner);
            consoleManager.usersMenuHandler(null);
        }

        System.out.print("Position: ");
        scanner.nextLine();
        String position = scanner.nextLine();
        System.out.print("Technologies: ");
        String technologies = scanner.nextLine();

        if (consoleManager.getManagementTool().createUser(userName, age, position, technologies) != null) {
            ConsoleManager.goBackHandler("User was created successfully.", scanner);
            consoleManager.usersMenuHandler(null);
        } else {
            ConsoleManager.goBackHandler("User creation error!", scanner);
            createUser();
        }
    }

    void showUsers() {
        System.out.println("Entire list of users...\n");

        Collection<User> usersList = consoleManager.getManagementTool().getUsersList();
        if (usersList.size() != 0) {
            for (User u : usersList)
                System.out.println(u + "\n");

            ConsoleManager.goBackHandler("", scanner);
            consoleManager.usersMenuHandler(null);
        } else {
            ConsoleManager.goBackHandler("We have no users yet!", scanner);
            consoleManager.usersMenuHandler(null);
        }
    }

    void goToUser() {
        System.out.println("Exist user searching...");

        System.out.print("User name: ");
        String userName = scanner.nextLine();

        User u = consoleManager.getManagementTool().getUserByName(userName);

        if (u != null) {
            existUserMenu(u);
        } else {
            System.out.printf("Sorry, we have no users by name %s!", userName);
            ConsoleManager.goBackHandler("", scanner);
            consoleManager.usersMenuHandler(null);
        }
    }

    void existUserMenu(final User u) {
        ConsoleMenu existUserMenu = new ConsoleMenu();

        existUserMenu.add("List of user projects", new ConsoleMenuCallback() {
            public void invoke() {
                listOfUserProjects(u);
            }
        });
        existUserMenu.add("List of user tasks", new ConsoleMenuCallback() {
            public void invoke() {
                listOfUserTasks(u);
            }
        });
        existUserMenu.add("Delete current user", new ConsoleMenuCallback() {
            public void invoke() {
                deleteCurrentUser(u);
            }
        });
        existUserMenu.add("Go back", new ConsoleMenuCallback() {
            public void invoke() {
                consoleManager.usersMenuHandler(null);
            }
        });
        existUserMenu.show(u.getName());
    }

    void deleteCurrentUser(User u) {
        System.out.println("Deleting current user...\n");

        System.out.println("[UNDER CONSTRUCTION]");
        ConsoleManager.goBackHandler("", scanner);
        existUserMenu(u);
    }

    void listOfUserProjects(User u) {
        System.out.printf("%s projects\n", u.getName());
        System.out.println("------------");

        System.out.println("[UNDER CONSTRUCTION]");
        ConsoleManager.goBackHandler("", scanner);
        existUserMenu(u);
    }

    void listOfUserTasks(User u) {
        System.out.printf("%s tasks\n", u.getName());
        System.out.println("------------");

        System.out.println("[UNDER CONSTRUCTION]");
        ConsoleManager.goBackHandler("", scanner);
        existUserMenu(u);
    }
}
